﻿using System;
using System.Windows.Forms;
using SdmxMl.Structure;
using SdmxMl.Panels;
using SdmxMl.Manager;

namespace SdmxMl.UC
{
    /// <summary>
    /// Control for Process step detail edition
    /// </summary>
    public partial class UcProcessStep : UcBase
    {
        #region Internal members

        private bool _isFinal;
        private ProcessStepType _step;

        #endregion

        #region Constructor

        public UcProcessStep()
        {
            InitializeComponent();
            comboBoxTitle.DropDownControl = ucMlText;
            ucObjectIdListInput.Text = "Input";
            ucObjectIdListOutput.Text = "Output";

            // Transition tab
            dataGridViewAnnot.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            SetGridTextWidth();
        }

        #endregion

        #region Binding

        public override bool BindFromNode(TreeNode node, bool isFinal)
        {
            _step = null;
            _isFinal = isFinal;
            base.BindFromNode(node, isFinal);
            if (node != null)
                _step = node.Tag as ProcessStepType;
            BindToControls();

            return Visible;
        }

        private void BindToControls()
        {
            if (_step != null)
            {
                Visible = true;
                textBoxValue.Text = _step.Id;
                comboBoxTitle.Text = _step.Name.ToString();
                ucMlText.ML = _step.Name;
                comboMultiLinesDescr.Text = _step.Description.ToString();
                comboMultiLinesDescr.ML = _step.Description;
                comboMultiLinesComput.Text = _step.Computation.ToString();
                comboMultiLinesComput.ML = _step.Computation;

                ucObjectIdListInput.ObjIdList = _step.InputList;
                ucObjectIdListOutput.ObjIdList = _step.OutputList;

                IsFinalEditControls(_isFinal);
                FillParentInfo(textBoxParent, textBoxParentCodeDescr);

                UpdateDataGrid(_step.TransitionList);


            }
            else
            {
                Visible = false;
            }
        }

        protected override bool BindFromControls()
        {
            bool err = false;
            if (_step != null)
            {
                ProcessStepTypeList psl = _step.InnerHierarchy as ProcessStepTypeList;
                err = CheckUniqueId(textBoxValue, psl.GetHierarchyManageOfItem(_step));
                //  ct.Id = textBoxId.Text;
                _step.Name.UpdateForCurrentLanguage(comboBoxTitle.Text);
                _step.Description.UpdateForCurrentLanguage(comboMultiLinesDescr.Text);
                _step.Computation.UpdateForCurrentLanguage(comboMultiLinesComput.Text);

            }
            return err;

        }

        // No edition if Parent Artefact IsFinal
        private void IsFinalEditControls(bool isFinal)
        {
            bool enabled = isFinal == false;

            textBoxValue.ReadOnly = isFinal;
            comboBoxTitle.IsFinal = isFinal;
            comboMultiLinesDescr.IsFinal = isFinal;
            comboMultiLinesComput.IsFinal = isFinal;

            ucObjectIdListInput.IsFinal = isFinal;
            ucObjectIdListOutput.IsFinal = isFinal;
        }

        #endregion

        #region Step Tab

        private void panelIO_Resize(object sender, EventArgs e)
        {
            int middle = this.panelMiddle.Width;
            int width = (this.Width / 2) - middle / 2;
            this.panelLeft.Width = width;
            this.panelRight.Width = width;
        }
        #endregion

        #region Transition Grid

        private void SetGridTextWidth()
        {
            dataGridViewAnnot.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            dataGridViewAnnot.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

        /// <summary>Update Grid buttons state</summary>
        /// <param name="enable">If true buttons are enabled</param>
        protected void EnableGrigButtons(bool enable)
        {
            butAdd.Enabled = enable && _isFinal == false;
            butEdit.Enabled = enable && dataGridViewAnnot.RowCount > 0;
            butRemove.Enabled = enable && _isFinal == false && dataGridViewAnnot.SelectedCells.Count > 0;
            buttonNavigateStep.Enabled = dataGridViewAnnot.RowCount > 0 && dataGridViewAnnot.SelectedCells.Count > 0;
        }

        private void UpdateDataGrid(TransitionTypeList tranList)
        {
            if (tranList != null)
            {
                dataGridViewAnnot.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
                dataGridViewAnnot.DataSource = tranList.AsDataTable();
                SetGridTextWidth();
                EnableGrigButtons(true);

            }
            else
            {
                dataGridViewAnnot.DataSource = null;
                EnableGrigButtons(false);
            }

        }

        private void butEdit_Click(object sender, EventArgs e)
        {
            DlgEditTransition aDlg = new DlgEditTransition(_step.RootHierarchy as ProcessStepTypeList);
            int rowIndex = 0;
            if (dataGridViewAnnot.SelectedCells.Count > 0)
                rowIndex = dataGridViewAnnot.SelectedCells[0].RowIndex;

            if (rowIndex < _step.TransitionList.Count)
            {
                aDlg.Transition = _step.TransitionList[rowIndex];
                aDlg.IsFinal = _isFinal;
                aDlg.ShowDialog(this);
                UpdateDataGrid(_step.TransitionList);
            }
        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            TransitionType tr = new TransitionType();
            tr.TargetStep = "UNDEFINED";
            _step.TransitionList.Add(tr);
            UpdateDataGrid(_step.TransitionList);
            dataGridViewAnnot.Rows[dataGridViewAnnot.Rows.Count - 1].Selected = true;
        }

        private void butRemove_Click(object sender, EventArgs e)
        {
            int rowIndex = 0;
            if (dataGridViewAnnot.SelectedCells.Count > 0)
            {
                rowIndex = dataGridViewAnnot.SelectedCells[0].RowIndex;
                if (rowIndex < _step.TransitionList.Count)
                {
                    _step.TransitionList.RemoveAt(rowIndex);
                    UpdateDataGrid(_step.TransitionList);
                }
            }

        }

        private void buttonNavigateStep_Click(object sender, EventArgs e)
        {
            if (dataGridViewAnnot.SelectedCells.Count > 0)
            {
                int rowIndex = dataGridViewAnnot.SelectedCells[0].RowIndex;
                if (rowIndex < _step.TransitionList.Count)
                {
                    string stepId = dataGridViewAnnot.Rows[rowIndex].Cells[0].Value.ToString();
                    if (stepId.Length > 0)
                    {
                        // try to locate object with this Id in Step
                        ProcessStepTypeList pstl = _step.RootHierarchy as ProcessStepTypeList;
                        ProcessStepType pst = pstl.LocateItemFromFullId(stepId, _step.RootHierarchy);
                        if (pst != null)
                            NavigationManager.RequestNavigation(pst);
                    }
                }
            }

        }


        #endregion

    }
}
